package cn.com.nr.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasStationDeptEntity;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasStationDeptService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.nr.entity.NrDiggingsEndangerEntity;
import cn.com.nr.service.NrDiggingsEndangerService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;

/**
 * 分析矿山信息
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2025-02-08 16:34:41
 */
@Controller
@RequestMapping("/nr/diggingsEndanger")
public class NrDiggingsEndangerController {
	@Autowired
	private NrDiggingsEndangerService nrDiggingsEndangerService;
	@Autowired
	private GasStationinfoService gasStationinfoService;
	@Autowired
	private HttpSessionService httpSessionService;
	@Autowired
	private GasStationDeptService gasStationDeptService;
	@Autowired
	private DeptService deptService;
	
	@ApiOperation(value = "确认分析")
	@PostMapping("/realDelete")
	@ResponseBody
	public DataResult realDelete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
		for (String id : ids) {
			if (TsBqUtils.isNotNull(id)) {
				nrDiggingsEndangerService.realRemoveById(id);
				gasStationinfoService.realRemoveById(id);
			}
		}
		return DataResult.success();
	}
	
	@ApiOperation(value = "确认分析")
	@PostMapping("/add")
	@ResponseBody
	public DataResult add(@RequestBody @ApiParam(value = "id集合") List<String> ids) {

		for (String id : ids) {
			NrDiggingsEndangerEntity entity = new NrDiggingsEndangerEntity();
			GasStationinfoEntity gasStationinfoEntity = gasStationinfoService.getById(id);
			BeanUtils.copyProperties(gasStationinfoEntity, entity);
			entity.setUpdateTime(new Date());
			entity.setUpdateId(httpSessionService.getCurrentUserId());

			nrDiggingsEndangerService.save(entity);
		}

		return DataResult.success();
	}

	@ApiOperation(value = "取消分析")
	@PostMapping("/delete")
	@ResponseBody
	public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
		for (String id : ids) {
			if (TsBqUtils.isNotNull(id)) {
				nrDiggingsEndangerService.realRemoveById(id);

			}
		}

		return DataResult.success();
	}

	@ApiOperation(value = "更新")
	@PutMapping("/update")
	@ResponseBody
	public DataResult update(@RequestBody NrDiggingsEndangerEntity nrDiggingsEndanger) {
		String id = nrDiggingsEndanger.getId();
		String currentUserId = httpSessionService.getCurrentUserId();
		Date date = new Date();

		GasStationinfoEntity gasStationinfoEntity = gasStationinfoService.getById(id);
		gasStationinfoEntity.setName(nrDiggingsEndanger.getName());
		gasStationinfoEntity.setTaxpayerCode(nrDiggingsEndanger.getTaxpayerCode());
		gasStationinfoEntity.setTaxpayerName(nrDiggingsEndanger.getTaxpayerName());
		gasStationinfoEntity.setTaxorgcode(nrDiggingsEndanger.getTaxorgcode());

		gasStationinfoEntity.setUpdateTime(date);
		gasStationinfoEntity.setUpdateId(currentUserId);

		gasStationinfoService.updateById(gasStationinfoEntity);

		nrDiggingsEndangerService.updateById(nrDiggingsEndanger);
		return DataResult.success();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据(运维)")
	@PostMapping("/list")
	@ResponseBody
	public DataResult list(@RequestBody GasStationinfoEntity vo) {
		String taxorgcode = vo.getTaxorgcode();

		String currentDeptId = httpSessionService.getCurrentDeptId();

		List<GasStationDeptEntity> list = gasStationDeptService.list();
		List<String> ids = new ArrayList<String>();
		for (GasStationDeptEntity gasStationDeptEntity : list) {
			String endangerId = gasStationDeptEntity.getEndangerId();
			String regionId = gasStationDeptEntity.getRegionId();
			ids.add(endangerId);
			ids.add(regionId);
		}

		List<String> _ids = ids.stream().distinct().collect(Collectors.toList());

		Page page = new Page(vo.getPage(), vo.getLimit());
		LambdaQueryWrapper<GasStationinfoEntity> queryWrapper = Wrappers.lambdaQuery();

		queryWrapper.eq(GasStationinfoEntity::getType, "group");
		if (TsBqUtils.isNotNull(taxorgcode)) {
			queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, taxorgcode);
		} else {

			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, currentDeptId);

			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapper.like(GasStationinfoEntity::getTaxorgcode, currentDeptId.substring(0, 5));

			} else {
				// 省局
			}
		}
		if (TsBqUtils.isNotNull(vo.getName())) {
			queryWrapper.like(GasStationinfoEntity::getName, vo.getName());
		}

		if (TsBqUtils.isNotNull(vo.getTaxpayerCode())) {
			queryWrapper.like(GasStationinfoEntity::getTaxpayerCode, vo.getTaxpayerCode());
		}

		queryWrapper.ne(GasStationinfoEntity::getPid, "0");
		queryWrapper.notIn(GasStationinfoEntity::getId, _ids);

		IPage<GasStationinfoEntity> iPage = gasStationinfoService.page(page, queryWrapper);

		List<GasStationinfoEntity> records = iPage.getRecords();

		for (GasStationinfoEntity gasStationinfoEntity : records) {
			if (gasStationinfoEntity.getType().equals("group") || gasStationinfoEntity.getType().equals("domain")) {
				gasStationinfoEntity.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
			}

			SysDept sysDept = deptService.getById(gasStationinfoEntity.getTaxorgcode());
			if (TsBqUtils.isNotNull(sysDept)) {
				String name = sysDept.getName();
				gasStationinfoEntity.setTaxorgName(name);

			}

			String id = gasStationinfoEntity.getId();
			NrDiggingsEndangerEntity diggingsEndangerEntity = nrDiggingsEndangerService.getById(id);
			if (TsBqUtils.isNotNull(diggingsEndangerEntity)) {
				gasStationinfoEntity.setIsAnalyseNrDiggings("Y");
				gasStationinfoEntity.setNrDiggingsTon1(diggingsEndangerEntity.getTon1());
				gasStationinfoEntity.setNrDiggingsTon2(diggingsEndangerEntity.getTon2());

			} else {
				gasStationinfoEntity.setIsAnalyseNrDiggings("N");
			}

		}

		return DataResult.success(iPage);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/collectList1")
	@ResponseBody
	public DataResult collectList1(@RequestBody NrDiggingsEndangerEntity nrDiggingsEndanger) {

		String taxorgcode = nrDiggingsEndanger.getTaxorgcode();
		String currentDeptId = httpSessionService.getCurrentDeptId();

		Page page = new Page(nrDiggingsEndanger.getPage(), nrDiggingsEndanger.getLimit());
		LambdaQueryWrapper<NrDiggingsEndangerEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		// queryWrapper.eq(NrDiggingsEndangerEntity::getId,
		// nrDiggingsEndanger.getId());
		// 查询条件示例
		if (TsBqUtils.isNotNull(taxorgcode)) {
			queryWrapper.eq(NrDiggingsEndangerEntity::getTaxorgcode, taxorgcode);
		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapper.eq(NrDiggingsEndangerEntity::getTaxorgcode, currentDeptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapper.like(NrDiggingsEndangerEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
			}
		}
		
		if (TsBqUtils.isNotNull(nrDiggingsEndanger.getName())) {
			queryWrapper.like(NrDiggingsEndangerEntity::getName, nrDiggingsEndanger.getName());
		}

		if (TsBqUtils.isNotNull(nrDiggingsEndanger.getTaxpayerCode())) {
			queryWrapper.eq(NrDiggingsEndangerEntity::getTaxpayerCode, nrDiggingsEndanger.getTaxpayerCode());
		}
		
		// queryWrapper.eq(NrDiggingsEndangerEntity::getIndustry, "铁矿采矿");
		
		queryWrapper.and((wrapper)->{
            wrapper.eq(NrDiggingsEndangerEntity::getIndustry,"铁矿采矿")
                    .or().isNull(NrDiggingsEndangerEntity::getIndustry)
                    .or().eq(NrDiggingsEndangerEntity::getIndustry, "");
        });

		
		
		IPage<NrDiggingsEndangerEntity> iPage = nrDiggingsEndangerService.page(page, queryWrapper);

		List<NrDiggingsEndangerEntity> records = iPage.getRecords();

		for (NrDiggingsEndangerEntity gasStationinfoEntity : records) {
			if (gasStationinfoEntity.getType().equals("group") || gasStationinfoEntity.getType().equals("domain")) {
				gasStationinfoEntity.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
			}

			SysDept sysDept = deptService.getById(gasStationinfoEntity.getTaxorgcode());
			if (TsBqUtils.isNotNull(sysDept)) {
				String name = sysDept.getName();
				gasStationinfoEntity.setTaxorgName(name);
			}
		}

		return DataResult.success(iPage);
	}
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/selectList1")
	@ResponseBody
	public DataResult selectList1(@RequestBody NrDiggingsEndangerEntity nrDiggingsEndanger) {

		String taxorgcode = nrDiggingsEndanger.getTaxorgcode();
		String currentDeptId = httpSessionService.getCurrentDeptId();

		Page page = new Page(nrDiggingsEndanger.getPage(), nrDiggingsEndanger.getLimit());
		LambdaQueryWrapper<NrDiggingsEndangerEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		// queryWrapper.eq(NrDiggingsEndangerEntity::getId,
		// nrDiggingsEndanger.getId());
		// 查询条件示例
		if (TsBqUtils.isNotNull(taxorgcode)) {
			queryWrapper.eq(NrDiggingsEndangerEntity::getTaxorgcode, taxorgcode);
		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapper.eq(NrDiggingsEndangerEntity::getTaxorgcode, currentDeptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapper.like(NrDiggingsEndangerEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
			}
		}
		
		if (TsBqUtils.isNotNull(nrDiggingsEndanger.getName())) {
			queryWrapper.like(NrDiggingsEndangerEntity::getName, nrDiggingsEndanger.getName());
		}

		if (TsBqUtils.isNotNull(nrDiggingsEndanger.getTaxpayerCode())) {
			queryWrapper.eq(NrDiggingsEndangerEntity::getTaxpayerCode, nrDiggingsEndanger.getTaxpayerCode());
		}
		
		// queryWrapper.eq(NrDiggingsEndangerEntity::getIndustry, "铁矿选矿");
		
		queryWrapper.and((wrapper)->{
            wrapper.eq(NrDiggingsEndangerEntity::getIndustry,"铁矿选矿")
                    .or().isNull(NrDiggingsEndangerEntity::getIndustry)
                    .or().eq(NrDiggingsEndangerEntity::getIndustry, "");
        });
		
		IPage<NrDiggingsEndangerEntity> iPage = nrDiggingsEndangerService.page(page, queryWrapper);

		List<NrDiggingsEndangerEntity> records = iPage.getRecords();

		for (NrDiggingsEndangerEntity gasStationinfoEntity : records) {
			if (gasStationinfoEntity.getType().equals("group") || gasStationinfoEntity.getType().equals("domain")) {
				gasStationinfoEntity.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
			}

			SysDept sysDept = deptService.getById(gasStationinfoEntity.getTaxorgcode());
			if (TsBqUtils.isNotNull(sysDept)) {
				String name = sysDept.getName();
				gasStationinfoEntity.setTaxorgName(name);
			}
		}

		return DataResult.success(iPage);
	}
	
}
